import pymysql


def sql_query(sql_text):
    conn = pymysql.connect(host="127.0.0.1", user="root", database="individual_tax", password="753951", charset="utf8")
    cursor = conn.cursor()
    sql_len = cursor.execute(sql_text)
    sql_tuplet = []
    for i in range(sql_len):
        sql_tuplet.append(list(cursor.fetchone()))
    cursor.close()
    conn.close()
    return sql_len, sql_tuplet


def sql_update(sql_text):
    conn = pymysql.connect(host="127.0.0.1", user="root", database="individual_tax", password="753951", charset="utf8")
    cursor = conn.cursor()
    cursor.execute(sql_text)
    conn.commit()
    cursor.close()
    conn.close()


def q(message):
    if message == 'm':
        sql_text = 'select id,comany_name,task_set, wait_task from messages;'
        sql_len, sql_typlet = sql_query(sql_text)
        return  sql_typlet
    if message == 'e':
        sql_text = 'select error, error_time from error_table;'
        sql_typlet = sql_query(sql_text)
        return sql_typlet

def times(message):
    message = eval(message)
    if len(message):

        sql_text = 'update time_table set moring_start_time=\'{}\',moring_end_time=\'{}\',night_start_time=\'{}\',' \
                   'night_end_time=\'{}\',' \
                   'run_time=\'{}\';'.format(message['moring_start_time'], message['moring_end_time'],
                                             message['night_start_time'], message['night_end_time'],
                                             message['run_time'])
        sql_update(sql_text)
        return '成功'

    else:
        return '错误'


